import { _decorator, Component, Node,game ,Prefab,instantiate,Color} from 'cc';
import {GameMain} from "./GameMain";
import {BrickParticle} from "./BrickParticle";
const { ccclass, property } = _decorator;

@ccclass('BrickParticleManager')
export class BrickParticleManager extends Component {

    @property({type:Prefab})
    particlePrefab:Prefab=null;

    private particlePool=[];

    public static instance:BrickParticleManager=null;

    public static readonly particlePoolCnt:number=400;

    onLoad()
    {
        BrickParticleManager.instance=this;
        this.initPool();
    }

    private initPool()
    {
        let particle=null;
        for(let i=0;i<BrickParticleManager.particlePoolCnt;i++)
        {
            particle=instantiate(GameMain.instance.brickParticlePrefab);
            this.particlePool.push(particle);
        }
    }

    public static getParticle()
    {
        return BrickParticleManager.instance.particlePool.pop();
    }

    public static putParticle(particle)
    {
        BrickParticleManager.instance.particlePool.push(particle);
    }

    public createNewParticleEffect(gridIdx:number)
    {
        let particle=instantiate(this.particlePrefab);

        //console.log("** XXXX GridIdx="+gridIdx);
        GameMain.instance.grids[gridIdx].node.addChild(particle);
        particle.setPosition(0,0,0);
        let particleComponent:BrickParticle=particle.getComponent("BrickParticle");
        particleComponent.play(GameMain.instance.grids[gridIdx].ball.c);
    }

    public createNewBlastEffect(parentNode:Node, c:Color)
    {
        let particle=instantiate(this.particlePrefab);

        //console.log("** XXXX GridIdx="+gridIdx);
        parentNode.addChild(particle);
        particle.setPosition(0,0,0);
        let particleComponent:BrickParticle=particle.getComponent("BrickParticle");
        particleComponent.blast(c);
    }
}

